home *** CD-ROM | disk | FTP | other *** search
/ PC Plus SuperCD (UK) 1998 August / PC Plus SuperCD 50b Issue 142 (CD142b) (August 1998).iso / handson / Java / SCAdventure / MainForm.java < prev    next >
Encoding:
Java Source  |  1998-04-29  |  13.4 KB  |  499 lines

  1. /**
  2.  * 
  3.  * You can add code anywhere in this file - except in the areas that are
  4.  * written by the editor. You should not change the relative ordering of
  5.  * the code.
  6.  * 
  7.  * You can remove this comment block or replace it with another.
  8.  * 
  9.  * @see        
  10.  * @version    
  11.  * @author    
  12.  */
  13.  
  14. import java.awt.*;
  15. import java.awt.event.*;
  16. import java.beans.*;
  17. import java.io.*;
  18. import java.net.*;
  19. import java.util.*;
  20. import com.supercede.forms.*;
  21.  
  22. public class MainForm extends SuperCedeFrame implements Serializable, AdventureConstants
  23. {
  24.  
  25.     void mainFormPreInit()
  26.     {
  27.         // You can add code anywhere in this method.
  28.         // This method is called PRIOR to form initialization.
  29.         // NOTE: The form has NOT been initialized yet. Do not modify the form itself in this method.
  30.     }
  31.  
  32.     void mainFormPostInit()
  33.     {
  34.         // You can add code anywhere in this method.
  35.         // This method is called AFTER form initialization.
  36.         
  37.         // Here I create a File Menu bar and items
  38.         
  39.           // create MenuBar
  40.           MenuBar menubar = new MenuBar();
  41.             // add File menu
  42.           Menu fileMenu = new Menu ("File");
  43.             // add Help menu
  44.           Menu helpMenu = new Menu ("Help");
  45.             // declare MenuItems
  46.           MenuItem exitItem;
  47.           MenuItem loadItem;
  48.           MenuItem saveItem;
  49.           MenuItem aboutItem;
  50.             // add MenuItems to File Menu
  51.           fileMenu.add(loadItem = new MenuItem("Load"));
  52.           fileMenu.add(saveItem = new MenuItem("Save"));
  53.           fileMenu.addSeparator();
  54.           fileMenu.add(exitItem = new MenuItem("Exit"));          
  55.             // add MenuItem to Help Menu
  56.           helpMenu.add(aboutItem = new MenuItem("About"));  
  57.             // add MenuBar to the Form
  58.           menubar.add(fileMenu);
  59.           menubar.add(helpMenu);
  60.           this.setMenuBar(menubar);
  61.           
  62.           // --- add ActionListeners for the MenuItems ---
  63.             // load
  64.         loadItem.addActionListener(new ActionListener() {
  65.            public void actionPerformed(ActionEvent e) {
  66.                LoadGame();
  67.                }
  68.           });          
  69.             // save
  70.         saveItem.addActionListener(new ActionListener() {
  71.            public void actionPerformed(ActionEvent e) {
  72.                SaveGame();
  73.                }
  74.           });
  75.           // exit
  76.           exitItem.addActionListener(new ActionListener() {
  77.            public void actionPerformed(ActionEvent e) {
  78.                System.exit(0);
  79.                }
  80.           });
  81.          // exit
  82.           aboutItem.addActionListener(new ActionListener() {
  83.            public void actionPerformed(ActionEvent e) {
  84.                ShowAboutDlg();
  85.                }
  86.           });
  87.  
  88.     }
  89.     // --- Here I define the methods called by
  90.     //     the menuItems
  91.     
  92.       // Save
  93.     void SaveGame()
  94.     {
  95.         String SaveFileName = "Test.sav";
  96.         FileDialog saveFileDialog1 = new java.awt.FileDialog(this, "Save",FileDialog.SAVE); 
  97.         saveFileDialog1.setFile("*.sav"); 
  98.         saveFileDialog1.show();
  99.           // SaveFileName is the name of the file returned by the dialog
  100.         SaveFileName = saveFileDialog1.getFile();        
  101.         if (saveFileDialog1.getFile() != null) // i.e. if the File Dialog Cancel button wasn't pressed
  102.         {
  103.          if (!SaveFileName.endsWith(".sav"))
  104.              displayBox.setText("Error: Wrong File type!\nBe sure to save this file with the extension: '.sav'");
  105.          else
  106.             displayBox.setText(Imp.SaveAdv(SaveFileName));            
  107.         }
  108.     }
  109.  
  110.       // Load    
  111.     void LoadGame()
  112.     {
  113.         // When Load, Load is selected, display File Open dialog
  114.         String LoadFileName = "Test.sav";
  115.         FileDialog openFileDialog1 = new java.awt.FileDialog(this, "Load",FileDialog.LOAD);         
  116.         openFileDialog1.setFile("*.sav"); 
  117.         openFileDialog1.show();
  118.         LoadFileName = openFileDialog1.getFile();
  119.         if (openFileDialog1.getFile() != null) // i.e. if the File Dialog Cancel button wasn't pressed
  120.         {
  121.         if (!LoadFileName.endsWith(".sav"))
  122.             displayBox.setText("Error: Wrong File type!\nOnly loads files with extension: '.sav'");
  123.         else
  124.            displayBox.setText(Imp.LoadAdv(LoadFileName));
  125.         }
  126.  
  127.     }
  128.  
  129.     void ShowAboutDlg()
  130.     {
  131.      try {  
  132.       (new AboutDlg(this)).show();
  133.       } catch(Exception ex) {
  134.         displayBox.setText("Can't show the About Dialog!");
  135.       }
  136.     }
  137.  
  138.  
  139.     
  140. public static void main(String args[])
  141.     {
  142.         // You can add code anywhere in this method.
  143.  
  144.         try
  145.         {
  146.             MainForm form = new MainForm();
  147.             form.setVisible(true);
  148.         }
  149.         catch(Throwable t)
  150.         {
  151.             System.out.println("Cannot construct the form: " + t);
  152.             System.exit(1);
  153.         }
  154.     }
  155.  
  156.  
  157.     public boolean mainFormWindowClosing(WindowEvent arg0)
  158.     {
  159.         // Put event handler code here. Return false for normal processing, true to override
  160.  
  161.         return false;
  162.     }
  163.  
  164.     public boolean mainFormWindowClosed(WindowEvent arg0)
  165.     {
  166.         // Put event handler code here. Return false for normal processing, true to override
  167.  
  168.         return false;
  169.     }
  170.  
  171.     public void stop()
  172.     {
  173.         // You can add code anywhere in this method.
  174.  
  175.         SuperCedeStop();   // Do not remove this line.
  176.     }
  177.  
  178.     public void start()
  179.     {
  180.         // You can add code anywhere in this method.
  181.  
  182.         SuperCedeStart();   // Do not remove this line.
  183.     }
  184.  
  185.     public void init()
  186.     {
  187.         // You can add code anywhere in this method.
  188.  
  189.         SuperCedeInit();   // Do not remove this line.
  190.     }
  191.     
  192. // -------------------------------------------------
  193. // ----- MY OWN HAND-WRITTEN CODE FROM HERE ON -----
  194. // -------------------------------------------------
  195.  
  196. // The Implementor class 'wraps' up all the internal details of
  197. // the game itself.
  198.     Implementer Imp = new Implementer();
  199.  // --- The displayBox textArea
  200.     void updatedisplayBox(String msg) {
  201.         // !!! argument is now a String - any special message returned
  202.         // by Adventure (e.g. "No Exit!\n")
  203.         //
  204.         // if msg <> "" display the message
  205.         // display description of room anyway
  206.         String s = msg;
  207.         if (s.equals("")) // if no special message show room description
  208.         {                 // else show message 
  209.             Room r = Imp.getAdv().getplayer().getroom();
  210.             Vector thingshere = r.getthings();
  211.             Vector inventory = Imp.getAdv().getplayer().getthings();
  212.                 s = "You are in " + 
  213.                 r.getname() + "\n" +
  214.                 r.getdescription() + "\n" +
  215.                 "--- Things here ---\n";
  216.                 for (Enumeration e = thingshere.elements(); e.hasMoreElements(); ) 
  217.                 s = s + ((Thing)e.nextElement()).getname() + ", ";                
  218.                 s = s + "\n--- You have ---\n";
  219.                 for (Enumeration e = inventory.elements(); e.hasMoreElements(); ) 
  220.                 s = s + ((Thing)e.nextElement()).getname() + ", ";
  221.         }
  222.         displayBox.setText( s + "\n" );
  223.     }
  224.  
  225.     public void NBtnMouseClicked(MouseEvent arg0)
  226.     {
  227.        updatedisplayBox(Imp.getAdv().movePlayerTo(NORTH));
  228.     }
  229.  
  230.     public void SBtnMouseClicked(MouseEvent arg0)
  231.     {
  232.         updatedisplayBox(Imp.getAdv().movePlayerTo(SOUTH));
  233.     }
  234.  
  235.     public void WBtnMouseClicked(MouseEvent arg0)
  236.     {
  237.         updatedisplayBox(Imp.getAdv().movePlayerTo(WEST));
  238.     }
  239.  
  240.     public void EBtnMouseClicked(MouseEvent arg0)
  241.     {
  242.         updatedisplayBox(Imp.getAdv().movePlayerTo(EAST));
  243.     }
  244.  
  245.     public void takeBtnMouseClicked(MouseEvent arg0)
  246.     {
  247.         updatedisplayBox(Imp.getAdv().takeOb(InputTF.getText()));
  248.     }
  249.  
  250.     public void dropBtnMouseClicked(MouseEvent arg0)
  251.     {
  252.         updatedisplayBox(Imp.getAdv().dropOb(InputTF.getText()));
  253.     }
  254.  
  255.     public void lookBtnMouseClicked(MouseEvent arg0)
  256.     {
  257.         updatedisplayBox("");
  258.     }
  259.  
  260.     // SuperCede Begin 2.0 Form Members
  261.     // Do not remove the Begin and End markers.
  262.     // The editor will rewrite the contents of this section each time the form is saved.
  263.  
  264.     // References to Beans within the Form.
  265.  
  266.     com.supercede.beans.stdawt.TextAreaSB displayBox;
  267.     TextField InputTF;
  268.     Button dropBtn;
  269.     Button lookBtn;
  270.     Button takeBtn;
  271.     Button NBtn;
  272.     Button WBtn;
  273.     Button EBtn;
  274.     Button SBtn;
  275.  
  276.     private void SuperCedeConstructor() throws IOException, ClassNotFoundException, ClassCastException, SuperCedeInvalidStateException
  277.     {
  278.         // Construct the actual connectors to give to our base class.
  279.  
  280.         Vector connectors = new Vector(9);
  281.         connectors.addElement(new MainFormWindowClosingConnector4(this));
  282.         connectors.addElement(new MainFormWindowClosedConnector5(this));
  283.         connectors.addElement(new MainFormEventConnector6(this));
  284.         connectors.addElement(new MainFormEventConnector7(this));
  285.         connectors.addElement(new MainFormEventConnector8(this));
  286.         connectors.addElement(new MainFormEventConnector9(this));
  287.         connectors.addElement(new MainFormEventConnector11(this));
  288.         connectors.addElement(new MainFormEventConnector12(this));
  289.         connectors.addElement(new MainFormEventConnector13(this));
  290.  
  291.         super.initializeThis(connectors);
  292.  
  293.         // Make references to Beans within the Form.
  294.  
  295.         int i = 0;
  296.  
  297.         displayBox = (com.supercede.beans.stdawt.TextAreaSB) getComponent(i++);
  298.         InputTF = (TextField) getComponent(i++);
  299.         dropBtn = (Button) getComponent(i++);
  300.         lookBtn = (Button) getComponent(i++);
  301.         takeBtn = (Button) getComponent(i++);
  302.         NBtn = (Button) getComponent(i++);
  303.         WBtn = (Button) getComponent(i++);
  304.         EBtn = (Button) getComponent(i++);
  305.         SBtn = (Button) getComponent(i++);
  306.     }
  307.  
  308.     private void SuperCedeInit()
  309.     {
  310.     }
  311.  
  312.     private void SuperCedeStart()
  313.     {
  314.     }
  315.  
  316.     private void SuperCedeStop()
  317.     {
  318.     }
  319.  
  320.     public MainForm() throws IOException, ClassNotFoundException, ClassCastException, SuperCedeInvalidStateException
  321.     {
  322.         super();
  323.  
  324.         mainFormPreInit();
  325.         SuperCedeConstructor();
  326.         mainFormPostInit();
  327.     }
  328.  
  329.     public void SuperCedeWindowClosing(WindowEvent arg0)
  330.     {
  331.         if (mainFormWindowClosing(arg0) == false)
  332.         {
  333.             dispose();
  334.         }
  335.     }
  336.  
  337.     public void SuperCedeWindowClosed(WindowEvent arg0)
  338.     {
  339.         if (mainFormWindowClosed(arg0) == false)
  340.         {
  341.             System.exit(0);
  342.         }
  343.     }
  344.  
  345.     // SuperCede End 2.0 Form Members
  346. }
  347.  
  348. // SuperCede Begin 2.0 Form Connectors
  349. // Do not remove the Begin and End markers.
  350. // The editor will rewrite the contents of this section each time the form is saved.
  351. // Connections for MainForm:
  352. //    MainFormWindowClosingConnector4: from MainForm.windowClosing to MainForm.SuperCedeWindowClosing
  353. //    MainFormWindowClosedConnector5: from MainForm.windowClosed to MainForm.SuperCedeWindowClosed
  354. //    MainFormEventConnector6: from NBtn.mouseClicked to MainForm.NBtnMouseClicked
  355. //    MainFormEventConnector7: from SBtn.mouseClicked to MainForm.SBtnMouseClicked
  356. //    MainFormEventConnector8: from WBtn.mouseClicked to MainForm.WBtnMouseClicked
  357. //    MainFormEventConnector9: from EBtn.mouseClicked to MainForm.EBtnMouseClicked
  358. //    MainFormEventConnector11: from takeBtn.mouseClicked to MainForm.takeBtnMouseClicked
  359. //    MainFormEventConnector12: from dropBtn.mouseClicked to MainForm.dropBtnMouseClicked
  360. //    MainFormEventConnector13: from lookBtn.mouseClicked to MainForm.lookBtnMouseClicked
  361.  
  362. final class MainFormWindowClosingConnector4 extends WindowAdapter implements SuperCedeConnector, Serializable
  363. {
  364.     private MainForm target;
  365.  
  366.     public MainFormWindowClosingConnector4(MainForm target)
  367.     {
  368.         this.target = target;
  369.     }
  370.  
  371.     public void windowClosing(WindowEvent arg0)
  372.     {
  373.         target.SuperCedeWindowClosing(arg0);
  374.     }
  375. }
  376.  
  377. final class MainFormWindowClosedConnector5 extends WindowAdapter implements SuperCedeConnector, Serializable
  378. {
  379.     private MainForm target;
  380.  
  381.     public MainFormWindowClosedConnector5(MainForm target)
  382.     {
  383.         this.target = target;
  384.     }
  385.  
  386.     public void windowClosed(WindowEvent arg0)
  387.     {
  388.         target.SuperCedeWindowClosed(arg0);
  389.     }
  390. }
  391.  
  392. final class MainFormEventConnector6 extends MouseAdapter implements SuperCedeConnector, Serializable
  393. {
  394.     private MainForm target;
  395.  
  396.     public MainFormEventConnector6(MainForm target)
  397.     {
  398.         this.target = target;
  399.     }
  400.  
  401.     public void mouseClicked(MouseEvent arg0)
  402.     {
  403.         target.NBtnMouseClicked(arg0);
  404.     }
  405. }
  406.  
  407. final class MainFormEventConnector7 extends MouseAdapter implements SuperCedeConnector, Serializable
  408. {
  409.     private MainForm target;
  410.  
  411.     public MainFormEventConnector7(MainForm target)
  412.     {
  413.         this.target = target;
  414.     }
  415.  
  416.     public void mouseClicked(MouseEvent arg0)
  417.     {
  418.         target.SBtnMouseClicked(arg0);
  419.     }
  420. }
  421.  
  422. final class MainFormEventConnector8 extends MouseAdapter implements SuperCedeConnector, Serializable
  423. {
  424.     private MainForm target;
  425.  
  426.     public MainFormEventConnector8(MainForm target)
  427.     {
  428.         this.target = target;
  429.     }
  430.  
  431.     public void mouseClicked(MouseEvent arg0)
  432.     {
  433.         target.WBtnMouseClicked(arg0);
  434.     }
  435. }
  436.  
  437. final class MainFormEventConnector9 extends MouseAdapter implements SuperCedeConnector, Serializable
  438. {
  439.     private MainForm target;
  440.  
  441.     public MainFormEventConnector9(MainForm target)
  442.     {
  443.         this.target = target;
  444.     }
  445.  
  446.     public void mouseClicked(MouseEvent arg0)
  447.     {
  448.         target.EBtnMouseClicked(arg0);
  449.     }
  450. }
  451.  
  452. final class MainFormEventConnector11 extends MouseAdapter implements SuperCedeConnector, Serializable
  453. {
  454.     private MainForm target;
  455.  
  456.     public MainFormEventConnector11(MainForm target)
  457.     {
  458.         this.target = target;
  459.     }
  460.  
  461.     public void mouseClicked(MouseEvent arg0)
  462.     {
  463.         target.takeBtnMouseClicked(arg0);
  464.     }
  465. }
  466.  
  467. final class MainFormEventConnector12 extends MouseAdapter implements SuperCedeConnector, Serializable
  468. {
  469.     private MainForm target;
  470.  
  471.     public MainFormEventConnector12(MainForm target)
  472.     {
  473.         this.target = target;
  474.     }
  475.  
  476.     public void mouseClicked(MouseEvent arg0)
  477.     {
  478.         target.dropBtnMouseClicked(arg0);
  479.     }
  480. }
  481.  
  482. final class MainFormEventConnector13 extends MouseAdapter implements SuperCedeConnector, Serializable
  483. {
  484.     private MainForm target;
  485.  
  486.     public MainFormEventConnector13(MainForm target)
  487.     {
  488.         this.target = target;
  489.     }
  490.  
  491.     public void mouseClicked(MouseEvent arg0)
  492.     {
  493.         target.lookBtnMouseClicked(arg0);
  494.     }
  495. }
  496.  
  497. // The following line must be the last line in the file.
  498. // SuperCede End 2.0 Form Connectors
  499.